package com.itpeng.controller;

import com.itpeng.constant.MessageConstant;
import com.itpeng.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.plugin.liveconnect.SecurityContextHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Aythor:彭彦钦
 * @Data: 2020/9/28  19:41
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 获得用户名
     * @return
     */
    @GetMapping("/getUsername")
    public Result getUsername(HttpServletRequest request, HttpServletResponse response){
        //通过security的全局上下文对象，来获得登录用户的认证信息
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //通过信息来得到登录用户的用户名
        String username = loginUser.getUsername();
        //将用户名返回给前端
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
    }

    @RequestMapping("/loginSuccess")
    public Result loginSuccess(){
        return new Result(true, MessageConstant.LOGIN_SUCCESS);
    }

    @RequestMapping("/loginFail")
    public Result loginFail(){
        return new Result(false, "用户名或密码不正确");
    }

}
